package demo1;

import java.util.Arrays;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //两个字符串的删除操作
    char[] s, t;
    int[][] memo;
    public int minDistance(String word1, String word2) {
        s = word1.toCharArray();
        t = word2.toCharArray();
        int m = word1.length(), n = word2.length();
        memo = new int[m][n];
        for (int[] row : memo) {
            Arrays.fill(row, -1);
        }
        return dfs(m - 1, n - 1);
    }
    private int dfs(int i, int j) {
        if (i < 0) {
            return j + 1;
        }
        if (j < 0) {
            return i + 1;
        }
        if (memo[i][j] != -1) {
            return memo[i][j];
        }
        if (s[i] == t[j]) {
            return memo[i][j] = dfs(i - 1, j - 1);
        }
        return memo[i][j] = Math.min(dfs(i - 1, j), dfs(i, j - 1)) + 1;
    }

    //设计相邻元素求和服务
    public static int[][] Grid;
    public int[][] move = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}};
    public int[][] diagona = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};

    public void NeighborSum(int[][] grid) {
        Grid = new int[grid.length][grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                Grid[i][j] = grid[i][j];
            }
        }
    }

    public int adjacentSum(int value) {
        int sum = 0;
        int x = 0;
        int y = 0;
        int row = Grid.length;
        int col = Grid[0].length;
        boolean flag = false;
        for (int i = 0; i < Grid.length; i++) {
            for (int j = 0; j < Grid.length; j++) {
                if (Grid[i][j] == value) {
                    x = i;
                    y = j;
                    flag = true;
                    break;
                }
            }
            if (flag) {
                break;
            }
        }
        for (int k = 0; k < move.length; k++) {
            int x1 = x + move[k][0];
            int y1 = y + move[k][1];
            if ((x1 >= 0 && x1 < row) && (y1 >= 0 && y1 < col)) {
                sum += Grid[x1][y1];
            }
        }
        return sum;
    }

    public int diagonalSum(int value) {
        int sum = 0;
        int x = 0;
        int y = 0;
        int row = Grid.length;
        int col = Grid[0].length;
        boolean flag = false;
        for (int i = 0; i < Grid.length; i++) {
            for (int j = 0; j < Grid.length; j++) {
                if (Grid[i][j] == value) {
                    x = i;
                    y = j;
                    flag = true;
                    break;
                }
            }
            if (flag) {
                break;
            }
        }
        for (int k = 0; k < diagona.length; k++) {
            int x1 = x + diagona[k][0];
            int y1 = y + diagona[k][1];
            if ((x1 >= 0 && x1 < row) && (y1 >= 0 && y1 < col)) {
                sum += Grid[x1][y1];
            }
        }
        return sum;
    }
}
